﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmEmployee_Editor : XtraForm
    {
        public FrmEmployee_Editor()
        {
            InitializeComponent();
        }
        /// <summary>
        /// ID,非整数位新增
        /// </summary>
        public long ID;
        /// <summary>
        /// 部门ID
        /// </summary>
        private int DepartmentID;
        /// <summary>
        /// 部门编号
        /// </summary>
        private string DepartmentCode;
        /// <summary>
        /// 标题格式
        /// </summary>
        private string captionFormat;
        private void FrmEmployee_Editor_Load(object sender, EventArgs e)
        {
            captionFormat = Text;
        }


        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ID <= 0)
            {
                return;
            }
            NewRecord();
        }

        private void btnFirst_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var bll = new DMZS.BLL.Employee();
            var row = bll.GetFirst();
            if (row != null)
            {
                ID = Convert.ToInt64(row["ID"]);
                LoadData();
            }
            else
            {
                NewRecord();
            }
        }

        private void btnPrevious_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ID <= 0)
            {
                return;
            }
            var bll = new DMZS.BLL.Employee();
            var row = bll.GetPrevious(ID);
            if (row != null)
            {
                ID = Convert.ToInt64(row["ID"]);
                LoadData();
            }
            else
            {
                LoadData();
            }
        }

        private void btnNext_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ID <= 0)
            {
                return;
            }
            var bll = new DMZS.BLL.Employee();
            var row = bll.GetNext(ID);
            if (row != null)
            {
                ID = Convert.ToInt64(row["ID"]);
                LoadData();
            }
            else
            {
                LoadData();
            }
        }

        private void btnLast_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var bll = new DMZS.BLL.Employee();
            var row = bll.GetLast();
            if (row != null)
            {
                ID = Convert.ToInt64(row["ID"]);
                LoadData();
            }
            else
            {
                NewRecord();
            }
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btnDepartment.Leave -= new EventHandler(btnDepartment_Leave);
            txtCode.Focus();
            btnDepartment.Leave += new EventHandler(btnDepartment_Leave);

            if (!(ValidateDepartment() && ValidateInput()))
            {
                return;
            }
            if (ID <= 0)
            {
                SavaNew();
            }
            else
            {
                SaveModify();
            }
        }
        /// <summary>
        /// 保存修改
        /// </summary>
        private void SaveModify()
        {
            var bll = new DMZS.BLL.Employee();
            var model = new DMZS.Model.Employee();

            model.ID = ID;
            model.Code = txtCode.Text.Trim();
            model.Name = txtName.Text.Trim();
            model.NamePY = CommonControl.StringHelper.GetFirstLetterOfChinese(model.Name, true);
            model.DepartmentID = DepartmentID;
            model.FullName = txtFullName.Text;
            model.Birthday = dteBirthday.DateTime;
            model.Address = txtAddress.Text.Trim();
            model.Telephone = txtTelephone.Text.Trim();
            model.Email = txtEmail.Text.Trim();
            model.IsStop = chkIsStop.Checked ? 1 : 0;
            model.Remark = mmoRemark.Text.Trim();


            var result = bll.UpdateExpand(model);
            if (result > 0)
            {
                XtraMessageBox.Show("保存成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                ID = result;
                LoadData();
            }
            else
            {
                if (result == -1000)
                {
                    XtraMessageBox.Show("编号重复。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtCode.Focus();
                    txtCode.SelectAll();
                }
                else
                {
                    if (result == -1001)
                    {
                        XtraMessageBox.Show("该部门类型不存在。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        btnDepartment.Focus();
                        btnDepartment.SelectAll();
                    }
                    else
                    {
                        XtraMessageBox.Show("该条数据已被删除。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        NewRecord();
                    }
                }
            }
        }
        /// <summary>
        /// 保存新记录
        /// </summary>
        private void SavaNew()
        {
            var bll = new DMZS.BLL.Employee();
            var model = new DMZS.Model.Employee();

            model.Code = txtCode.Text.Trim();
            model.Name = txtName.Text.Trim();
            model.NamePY = CommonControl.StringHelper.GetFirstLetterOfChinese(model.Name, true);
            model.DepartmentID = DepartmentID;
            model.FullName = txtFullName.Text;
            model.Birthday = dteBirthday.DateTime;
            model.Address = txtAddress.Text.Trim();
            model.Telephone = txtTelephone.Text.Trim();
            model.Email = txtEmail.Text.Trim();
            model.IsStop = chkIsStop.Checked ? 1 : 0;
            model.Remark = mmoRemark.Text.Trim();

            var result = bll.AddExpand(model);
            if (result > 0)
            {
                XtraMessageBox.Show("保存成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                ID = result;
                LoadData();
            }
            else
            {
                if (result == -1000)
                {
                    XtraMessageBox.Show("编号重复。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtCode.Focus();
                    txtCode.SelectAll();
                }
                else
                {
                    if (result == -1001)
                    {
                        XtraMessageBox.Show("该部门类型不存在。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        btnDepartment.Focus();
                        btnDepartment.SelectAll();
                    }
                }
            }
        }
        /// <summary>
        /// 检查输入
        /// </summary>
        /// <returns></returns>
        private bool ValidateInput()
        {
            if (btnDepartment.Text.Trim().Length == 0)
            {
                XtraMessageBox.Show("请输入部门编号。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                btnDepartment.Focus();
                return false;
            }
            else
            {
                if (txtCode.Text.Trim().Length == 0)
                {
                    XtraMessageBox.Show("请输入职工编号。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    txtCode.Focus();
                    return false;
                }
                else
                {
                    if (txtName.Text.Trim().Length == 0)
                    {
                        XtraMessageBox.Show("请输入职工名称。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                        txtName.Focus();
                        return false;
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 检查部门
        /// </summary>
        /// <returns></returns>
        private bool ValidateDepartment()
        {
            var bll = new DMZS.BLL.DepartmentNew();
            var row = bll.GetRecordByCode(btnDepartment.Text.Trim());
            if (row == null)
            {
                XtraMessageBox.Show("该部门类型不存在。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                DepartmentID = 0;
                DepartmentCode = string.Empty;
                txtDepartmentName.Text = string.Empty;
                btnDepartment.Focus();
                btnDepartment.SelectAll();
                return false;
            }
            else
            {
                DepartmentID = Convert.ToInt32(row["ID"]);
                DepartmentCode = row["Code"].ToString();
                txtDepartmentName.Text = row["Name"].ToString();
                return true;
            }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ID <= 0)
            {
                NewRecord();
                return;
            }
            else
            {
                if (DialogResult.Yes == XtraMessageBox.Show("确认删除?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                {
                    var bll = new DMZS.BLL.Employee();
                    bll.Delete(ID);
                    NewRecord();
                }
            }
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
        }

        private void FrmEmployee_Editor_Shown(object sender, EventArgs e)
        {
            if (ID <= 0)
            {
                NewRecord();
            }
            else
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            this.Text = string.Format(captionFormat, "维护");
            var bll = new DMZS.BLL.Employee();
            var row = bll.GetRecordByID(ID);
            if (row == null)
            {
                XtraMessageBox.Show("该条数据已被删除，请刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                Close();
                return;
            }

            DepartmentID = Convert.ToInt32(row["DepartmentID"]);
            DepartmentCode = row["DepartmentCode"].ToString();
            btnDepartment.Text = row["DepartmentCode"].ToString();
            txtDepartmentName.Text = row["DepartmentName"].ToString();
            txtCode.Text = row["Code"].ToString();
            txtName.Text = row["Name"].ToString();
            txtFullName.Text = row["FullName"].ToString();
            dteBirthday.DateTime = Convert.ToDateTime(row["Birthday"]);
            chkIsStop.Checked = Convert.ToBoolean(row["IsStop"]);

            txtAddress.Text = row["Address"].ToString();
            txtTelephone.Text = row["Telephone"].ToString();
            txtEmail.Text = row["Email"].ToString();
            mmoRemark.Text = row["Remark"].ToString();

            btnDepartment.Properties.ReadOnly = true;
            btnDepartment.TabStop = false;
            btnDepartment.Properties.Buttons[0].Enabled = false;
            txtCode.Properties.ReadOnly = true;
            txtCode.TabStop = false;
            txtName.Focus();
        }

        private void NewRecord()
        {
            Text = string.Format(captionFormat, "新增");
            ID = 0;
            DepartmentID = 0;
            DepartmentCode = string.Empty;
            btnDepartment.Text = string.Empty;
            txtDepartmentName.Text = string.Empty;
            txtCode.Text = string.Empty;
            txtName.Text = string.Empty;
            txtFullName.Text = string.Empty;
            dteBirthday.DateTime = Convert.ToDateTime("1970-01-01");
            chkIsStop.Checked = false;

            txtAddress.Text = string.Empty;
            txtTelephone.Text = string.Empty;
            txtEmail.Text = string.Empty;
            mmoRemark.Text = string.Empty;

            btnDepartment.Properties.ReadOnly = false;
            btnDepartment.TabStop = true;
            btnDepartment.Properties.Buttons[0].Enabled = true;
            txtCode.Properties.ReadOnly = false;
            txtCode.TabStop = true;
            btnDepartment.Focus();
        }

        private void btnDepartment_Leave(object sender, EventArgs e)
        {
            if (DepartmentCode == btnDepartment.Text.Trim())
            {
                return;
            }
            ValidateDepartment();
        }

        private void btnDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DialogForm.FrmDepartment_dlg frm = new DialogForm.FrmDepartment_dlg();
            frm.ReturnList += new DialogForm.FrmDepartment_dlg.ReturnListEventHandler(frm_ReturnList);
            frm.ShowDialog();
        }

        void frm_ReturnList(List<DataRow> lists)
        {
            if (lists == null) return;

            DepartmentID = Convert.ToInt32(lists[0]["ID"]);
            btnDepartment.Text = lists[0]["Code"].ToString();
            txtDepartmentName.Text = lists[0]["Name"].ToString();
        }
    }
}
